﻿Imports System.IO
Imports System.Threading

Public Class Form1
#Region "Declarations"
    Private totalstocks = 200
    Public arrayOfCharts(totalstocks) As Stock
    Private chartTotalSize As Integer = 0
    Private currentChartCon As Integer = 0
    Public Shared debugging As Boolean = False
    Public interval As Integer = 0
    Public maxinterval = 10
    Declare Function AllocConsole Lib "Kernel32.dll" () As Integer
    Declare Function AttachConsole Lib "Kernel32.dll" (ByVal dwProcessId As UInt32) As Integer
    Declare Function FreeConsole Lib "Kernel32.dll" () As Integer
    Dim p As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess
    Public FontColor As String
    Public BackColory As String
    Dim location2 As String = My.Application.Info.DirectoryPath
    Dim _buttondown
    Dim _buttonup
    Dim _buttonleave
#End Region
#Region "Events"
#Region "Buttons"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        currentChartCon = count(arrayOfCharts)
        arrayOfCharts(currentChartCon).stoc = TextBox1.Text
        getStock(currentChartCon)


        chartTotalSize += 1
        Label1.Text = currentChartCon + 1 & "/" & chartTotalSize
        If debugging Then
            Console.WriteLine("Button1 Clicked")
        End If
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            For i = (currentChartCon) To (count(arrayOfCharts) - 1)
                arrayOfCharts(i) = arrayOfCharts(i + 1)
            Next
            chartTotalSize -= 1
            currentChartCon -= 1
            getStock(currentChartCon)
            Label1.Text = currentChartCon + 1 & "/" & chartTotalSize
            If debugging Then
                Console.WriteLine("Removed")
            End If
        Catch ex As Exception
            If debugging Then
                Console.WriteLine("Failed to Remove")
            End If
        End Try

    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Not currentChartCon = 0 Then
            currentChartCon -= 1
            getStock(currentChartCon)
        End If
        Label1.Text = currentChartCon + 1 & "/" & chartTotalSize
        If debugging Then
            Console.WriteLine("Button2 Clicked")
        End If
    End Sub



    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Not currentChartCon = count(arrayOfCharts) - 1 Then
            currentChartCon += 1
            getStock(currentChartCon)
        End If
        Label1.Text = currentChartCon + 1 & "/" & chartTotalSize
        If debugging Then
            Console.WriteLine("Button3 Clicked")
        End If
    End Sub
#End Region
#Region "Toolstrip"
    Private Sub SettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        Form3.Show()
    End Sub
    Private Sub SaveCurrentStocksToWatchlistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCurrentStocksToWatchlistToolStripMenuItem.Click
        Dim text = Nothing
        Dim c As Integer = count(arrayOfCharts)
        For i As Integer = 0 To c
            text += arrayOfCharts(i).stoc & ","
        Next
        If Not Directory.Exists("C:\settings") Then
            Directory.CreateDirectory("C:\settings")
        End If
        SaveFileDialog1.InitialDirectory = "C:\settings\"
        SaveFileDialog1.ShowDialog()
        Dim aac = SaveFileDialog1.FileName
        Dim fs As New FileStream(aac, FileMode.Create)
        Dim fr As New StreamWriter(fs)
        fr.Write(text)
        fr.Close()
        fs.Close()
        If debugging Then
            Console.WriteLine("Saved Stocks to Watchlist")
        End If
    End Sub

    Private Sub OpenWatchlistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenWatchlistToolStripMenuItem.Click
        OpenFileDialog1.InitialDirectory = "C:\settings\"
        OpenFileDialog1.ShowDialog()
        Dim aaa = OpenFileDialog1.FileName
        Dim fs As New FileStream(aaa, FileMode.Open)
        Dim fr As New StreamReader(fs)
        Dim c = fr.ReadToEnd()
        fr.Close()
        fs.Close()
        Dim a As String() = c.Split(",")
        For Each o As Stock In arrayOfCharts
            o.stoc = ""
        Next
        For p As Integer = 0 To a.Count - 1
            arrayOfCharts(p).stoc = a(p)
        Next
        chartTotalSize = a.Count
        currentChartCon = a.Count - 1
        getStock(currentChartCon)
        Label1.Text = currentChartCon + 1 & "/" & chartTotalSize
        If debugging Then
            Console.WriteLine("Watchlist Opened")
        End If
    End Sub

    Private Sub PasteVWatchlistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteVWatchlistToolStripMenuItem.Click
        Dim a As String() = Clipboard.GetText().Split(",")
        For Each o As Stock In arrayOfCharts
            o.stoc = ""
        Next
        For p As Integer = 0 To a.Count - 1
            arrayOfCharts(p).stoc = a(p)
        Next
        chartTotalSize = a.Count
        currentChartCon = a.Count - 1
        getStock(currentChartCon)
        Label1.Text = currentChartCon + 1 & "/" & chartTotalSize
        If debugging Then
            Console.WriteLine("Pasted Virtual Watchlist")
        End If
    End Sub

    Private Sub EditWatchlistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditWatchlistToolStripMenuItem.Click
        Dim frm2 As New Form2
        frm2.Show()
        If debugging Then
            Console.WriteLine("Editing Watchlist")
        End If
    End Sub

    Private Sub CopyCurrentWatchlistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyCurrentWatchlistToolStripMenuItem.Click
        Dim a As String = Nothing

        For o As Integer = 0 To count(arrayOfCharts)
            a += arrayOfCharts(o).stoc + ","
        Next
        Clipboard.SetText(a)
        If debugging Then
            Console.WriteLine("Copied Watchlist")
        End If
    End Sub



    Private Sub StartDebuggingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartDebuggingToolStripMenuItem.Click

        If debugging = False Then
            StartDebuggingToolStripMenuItem.Text = "Stop Logging"
            'AttachConsole(p.Id)
            AllocConsole()
            Console.WriteLine("Console Allocated")
        Else
            Console.WriteLine("Console Closing")
            FreeConsole()
            StartDebuggingToolStripMenuItem.Text = "Start Logging"
        End If
        debugging = Not debugging
    End Sub
#End Region
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To totalstocks
            arrayOfCharts(i) = New Stock
        Next
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
GetSetting:
        Try
            FontColor = GetSetting("Stockviewer", "Font", "Color")
            BackColory = GetSetting("Stockviewer", "Forms", "Color")
        Catch ex As Exception
            SaveSetting("Stockviewer", "Font", "Color", Color.Black.ToString)
            SaveSetting("Stockviewer", "Forms", "Color", Color.White.ToString)
            GoTo GetSetting
        End Try
        If Not BackColory = "" Then
            Try
                BackColor = ColorTranslator.FromHtml(BackColory)
            Catch ex As Exception

            End Try

        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If interval = 0 Then
            Text = "sTockvIewer"
        End If
        If interval = 1 Then
            Text = "stOckviEwer"
        End If
        If interval = 2 Then
            Text = "stoCkvieWer"
        End If
        If interval = 3 Then
            Text = "stocKvieweR"
        End If
        If interval = 4 Then
            Text = "StockViewer"
        End If
        ' If interval = 5 Then
        'Text = "stockvIewer"
        'End If
        'If interval = 6 Then
        'Text = "stockviEwer"
        'End If
        'If interval = 7 Then
        '        Text = "stockvieWer"
        '       End If
        '      If interval = 8 Then
        'Text = "stockviewEr"
        'End If
        'If interval = 9 Then
        'Text = "stockvieweR"
        'End If
        'If interval = 10 Then
        'Text = "Stockviewer"
        'End If
        If interval < 4 Then
            interval += 1
        Else
            interval = 0
        End If
    End Sub
#End Region
#Region "Functions and subs"
    Private Sub getStock(ByVal index As Integer)

        PictureBox1.Image = arrayOfCharts(index).getStockImage()
        If debugging Then
            Console.WriteLine("Got Stock")
        End If
    End Sub
    Private Function count(ByVal array As Stock()) As Integer
        If debugging Then
            Console.WriteLine("Counting Stocks")
        End If
        For i As Integer = 0 To totalstocks
            If arrayOfCharts(i).stoc = "" Then
                Return i
            End If

        Next
        Return totalstocks + 1
    End Function
#End Region

End Class